// *** LICENSE HEADER ***
// Filename: ./player.cpp
// This file is part of Nodewarz 0.1
// Nodewarz is a 3d OpenGL strategy game
// Copyright (C) 2007 CZestmyr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// *** END LICENSE HEADER ***
#include "player.h"

player::player() {
    type = PT_LOCAL;
    nodes = 0;
    selected = 0;
    theLightManager = 0;
    clientNumber = 0;
    lost = false;
}

player::~player() {
    if (nodes)
        delete nodes;
}

void player::ResetData() {
    if (nodes)
        delete nodes;
    nodes = new nodeGraph(this);
    credits = 100;
    selected = 0;
    lost = false;
}

void player::SetLightManager(lightManager* newManager) {
    theLightManager = newManager;
    if (nodes)
        nodes->SetLightManager(newManager);
}

void player::SetColor(GLubyte red, GLubyte green, GLubyte blue) {
    color.r = red;
    color.g = green;
    color.b = blue;
    //Update lighting
    nodes->ResetLights();
}

SDL_Color player::GetColor() {
    return color;
}

node* player::AddNode(float posX, float posY) {
    return nodes->AddNode(posX, posY);
}

void player::RemoveNode(node* toRemove) {
    toRemove->DisconnectAll();
    nodes->RemoveNode(toRemove);
}

node* player::GetClosestNode(float posX, float posY) {
    return nodes->GetClosestNode(posX, posY);
}

node* player::GetSelectedNode() {
    return selected;
}

void player::SelectNode(float posX, float posY) {
    if (selected)
        selected->Deselect();
    selected = nodes->GetClosestNode(posX, posY);
    if (selected)
        selected->Select();
}

void player::DeselectSelected() {
    if (selected)
        selected->Deselect();
    selected = 0;
}

void player::AddConnection(node* nodeA, node* nodeB) {
    nodeA->Connect(nodeB, true);
    nodeB->Connect(nodeA, false);
}

void player::ResetGrowthData(){
    if (nodes)
        nodes->ResetGrowthData();
}

void player::NewRound() {
    if (nodes){
        nodes->Grow();
    }
}

int player::GetCredits() {
    return credits;
}

void player::SetCredits(int newCredits){
    credits = newCredits;
}

void player::SubtractCredits(int cost) {
    credits -= cost;
    if (credits < 0) credits = 0;
}

void player::AddCredits(int income){
    credits += income;
}

void player::SetType(playerType newType){
    type = newType;
}

playerType player::GetType(){
    return type;
}

void player::SetClientNumber(int newClientNumber){
    clientNumber = newClientNumber;
}

int player::GetClientNumber(){
    return clientNumber;
}

bool player::HasLost(){
    if (nodes->IsEmpty()) return true;
    else return false;
}

void player::Draw(bool showBoundaries, bool friendly, bool showEnergies) {
    //Select the player color and disable blending or textures
    glColor3ub(color.r, color.g, color.b);
    glDisable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);
    //Draw all nodes
    if (nodes)
        nodes->Draw(showBoundaries, friendly, showEnergies);
}
